package com.java.admin.admin.controller;

import com.java.admin.admin.service.LoginService;
import com.java.database.entity.Caradmininfo;
import com.java.database.entity.GenericResult;
import com.java.secnity.Authorrization;
import com.java.secnity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * 管理员信息
 */
@Controller
public class AdminController {
    @Autowired
    private LoginService loginService;

    @RequestMapping("/login")
    public String Login(String  adminName,String adminPwd, HttpServletRequest request){
        Caradmininfo caradmininfo = loginService.login(adminName, adminPwd);
        HttpSession session = request.getSession();
        session.setAttribute("session_user_key",caradmininfo);
        //存入权限信息
        session.setAttribute("user_role_key",caradmininfo.getAdminauthority());
        return "redirect:/admin/index.html";
    }

    /**
     * 查询管理员信息
     * @param request
     * @return
     */
    //如果管理员没有admin权限就不能查询管理员详细信息
    @ResponseBody
    @RequestMapping("/queryAdmin")
    public Caradmininfo queryAdmin(HttpServletRequest request){
        HttpSession session = request.getSession();
        Caradmininfo session_user_key = (Caradmininfo) session.getAttribute("session_user_key");
        String adminName = session_user_key.getAdminname();
        Caradmininfo caradmininfo = loginService.queryAdmin(adminName);
        return caradmininfo;
    }

    /**
     *通过管理员账户修改管理员信息
     */
    @RequestMapping("/updateadmin")
    public String updateadmin(Caradmininfo caradmininfo){
        System.out.println(caradmininfo.getAdminname());
        loginService.updateadmininfo(caradmininfo);
        return "redirect:/admin/admininfo.html";
    }


    /**
     * 添加管理员，必须有admin权限的管理员才能添加
     */
    @Authorrization(value = Role.ADMIN)
    @RequestMapping("/addadmin")
    public String addadmin(Caradmininfo caradmininfo){
         loginService.addadmin(caradmininfo);
         return "redirect:/admin/admininfo.html";
    }
}